package com.zhiqingchun.dao.account.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.zhiqingchun.dao.BaseDao;
import com.zhiqingchun.dao.account.IUserDao;
import com.zhiqingchun.dao.account.UserResult;
import com.zhiqingchun.dao.article.ArticleResult;
import com.zhiqingchun.pojo.Usr;

public class UserDao extends BaseDao implements IUserDao {

	@Override
	public UserResult login(Usr usr) {
		String sql = "select * from usr where email = ? and pwd = ?";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, usr.getEmail());
			ps.setString(2, usr.getPwd());
			ResultSet rs = ps.executeQuery();
			UserResult userResult = new UserResult();
			if (rs.next()) {
				userResult.setSuccess(true);
				usr.setUsr(rs.getString("usr"));
				usr.setId(rs.getInt("ID"));
				userResult.setUsr(usr);
			} else {
				userResult.setMessage("用户名或者密码错误");
			}
			return userResult;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}

	@Override
	public UserResult add(Usr usr) {
		String sql = "insert into usr (email, usr, pwd) values (?, ?, ?)";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			ResultSet rs = null;
			ps.setString(1, usr.getEmail());
			ps.setString(2, usr.getUsr());
			ps.setString(3, usr.getPwd());
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
			if (rs.next()) {
				usr.setId(rs.getInt(1));
			}
			ps.close();
			UserResult userResult = new UserResult();
			userResult.setSuccess(true);
			userResult.setUsr(usr);
			return userResult;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}

	@Override
	public UserResult byId(int id) {
		String sql = "select * from usr where id = ?";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			UserResult userResult = new UserResult();
			if (rs.next()) {
				userResult.setSuccess(true);
				Usr usr = new Usr();
				usr.setUsr(rs.getString("usr"));
				usr.setId(rs.getInt("ID"));
				usr.setIntro(rs.getString("intro"));
				usr.setCity(rs.getInt("city"));
				usr.setAvatar(rs.getString("avatar"));
				usr.setEmail(rs.getString("email"));
				usr.setDt(rs.getTimestamp("dt"));
				userResult.setUsr(usr);
			} else {
				userResult.setMessage("没有查询到用户ID为" + id + "的信息");
			}
			return userResult;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}

	@Override
	public UserResult update(Usr usr) {
		String sql = "update usr set usr = ?, avatar = ?, intro = ?, city = ? where id = ?";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, usr.getUsr());
			ps.setString(2, usr.getAvatar());
			ps.setString(3, usr.getIntro());
			ps.setInt(4, usr.getCity());
			ps.setInt(5, usr.getId());
			ps.executeUpdate();
			ps.close();
			
			UserResult userResult = new UserResult();
			userResult.setSuccess(true);
			userResult.setUsr(usr);
			return userResult;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}


}
